<?php

/**
 * 后台框架及登录模块
 * 
  + -----------------------------------------------------
 * Date 2013-3-8
  + -----------------------------------------------------
 */
class PublicAction extends AdminAction {

    /**
     * 检查登录
     */
    protected function checkLogin() {
        if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
            echo "<script>alert('登录超时，请重新登录！');top.location.href='" . __URL__ . "/login';</script>";
            exit;
            //$this->error('亲，您已经登录超时或是还没有登录，请重新登录！',__URL__.'/login');
        }
    }

    /**
     * 登录操作
     */
    public function login() {
        if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
            $this->display();
        } else {
            $this->redirect('Index/index');
        }
    }

    /**
     * 登录验证
     */
    public function doLogin() {
        $Admin = D("Admin");
        if (!$Admin->create()) {
            $this->error($Admin->getError());
        } else {
            /* if($_POST['captcha']){
              if($_SESSION['code']!=md5(strtoupper($_POST['captcha']))){
              $this->error('验证码错误，请重新输入！');
              }
              }else{
              $this->error('请输入验证码，谢谢！');
              } */
            $condition = array(
                'username' => $_POST['username'],
                'password' => strtoupper(md5(strtoupper(md5($_POST['password'])))),
                'is_admin' => 1
            );
            $admin     = $Admin->where($condition)->find();
            if ($admin) {
                $array = array(
                    'login_times'                 => $admin['login_times'] + 1,
                    'last_login_ip'               => get_real_ip(),
                    'last_login_time'             => time()
                );
                $update                       = $Admin->where("id=$admin[id]")->save($array);
                $_SESSION['AdminInfo']        = $admin;
                unset($_SESSION['AdminInfo']['password']);
                $_SESSION[C('USER_AUTH_KEY')] = $admin['id'];
                if ($admin['is_admin']) {
                    $_SESSION[C('ADMIN_AUTH_KEY')] = true;
                }
                //记录日志
                $this->success('登录成功！');
            } else {
                $this->error('登录失败，用户名或密码错误或你非管理员！');
            }
        }
    }

    /**
     * 清除缓存
     */
    public function clearCache() {
        $valid = 0;
        $type  = $_GET['type'];
        switch ($type) {
            case 1: $path = CACHE_PATH;
                break;
            case 2: $path = DATA_PATH;
                break;
            case 3: $path = LOG_PATH;
                break;
            case 4: $path = TEMP_PATH;
                break;
            default: $path = APP_PATH . 'Runtime/';
                break;
        }
        if ($path) {
            clearCache($path);
            $valid = 1;
        }
        $this->ajaxReturn(1, '清除缓存成功！', $valid);
    }

    public function logout() {
        $this->loginOut();
    }

    /**
     * 退出登录
     */
    public function loginOut() {
        $_SESSION = array();
        unset($_SESSION[C('USER_AUTH_KEY')]);
        unset($_SESSION[C('ADMIN_AUTH_KEY')]);
        unset($_SESSION['AdminInfo']);

        $this->redirect(__URL__ . '/login');
    }

    /**
     * 验证码生成
     * 用的自己的验证码库
     */
    public function buildCode() {
        createCaptcha(30, 80, 4, 15, array(255, 255, 255), false, false);
    }

    /**
     * 顶部页面
     */
    public function top() {
        $this->checkLogin();
        $Week = array("日", "一", "二", "三", "四", "五", "六");
        $date = '<span id="show_time"><script>show_time();</script></span> 星期' . $Week[date("w", time())];
        $this->assign('date', $date);
        $this->display();
    }

    /**
     * 菜单页面
     */
    public function menu() {
        $this->checkLogin();
        $menus = $this->getLeftMenu();
        $this->assign('menus', $menus);
        $this->display();
    }

    /**
     * 左侧
     */
    public function getLeftMenu($pid = 0) {
        $menu  = M('Menu');
        $menus = $menu->table(C("DB_PREFIX") . "menu m")
                ->join("left join " . C("DB_PREFIX") . "menu_type t on m.type_id=t.id")
                ->field("m.*,t.name,t.model")
                ->where("m.parent_id=$pid")
                ->order("m.sort_order asc")
                ->select();
        $array = array();
        if (is_array($menus) && count($menus) > 0) {
            foreach ($menus as $k => $v) {
                $array[$k] = $v;
                $submenus  = $this->getLeftMenu($v['id']);
                if (empty($submenus)) {
                    $array[$k]['submenus'] = array($v);
                } else {
                    //$array[$k]['submenus']=array_merge($submenus,array($v));
                    $array[$k]['submenus'] = $submenus;
                }
            }
        }
        return $array;
    }

    /**
     * 分隔页面
     */
    public function drag() {
        $this->checkLogin();
        $this->display();
    }

    /**
     * 底部页面
     */
    public function bot() {
        $this->checkLogin();
        $this->display();
    }

}

?>